import { Component, OnInit,ViewChild} from '@angular/core';
import {DrawService,TipService, ModalTipService,DcTablePagingPipe,ModalService,HttpService} from 'itcloud-plugin';
import { ViewNoticeComponent } from './view-notice/view-notice.component';
import { GetPathService } from '../get-path.service';

@Component({
  selector: 'app-my-done',
  templateUrl: './my-done.component.html',
  styleUrls: ['./my-done.component.css']
})
export class MyDoneComponent implements OnInit {
    //分页
    pager;
    param:any = {
        searchType:0,
        limit: 10,
        page: 1
    }
    cookies:any;
    tableId: string;
    tableLoading: boolean;
    tableOptions = {
        // maxHeight: '200px',
        // minHeight: '100px',
        checkbox: false,
        loading: true,
        showIndex: false
    };
    constructor(
    private drawService: DrawService,
    private modal: ModalService,
    private http:HttpService,
    private GetPathService:GetPathService,
    private tipService:TipService
    ) { }
headerConfig = [
  {
      field: 'infoCode', // 显示字段
      title: '公告编号', // 表头显示
      canSort: false, // 是否可排序
      width: '10%', // 单元格宽度
      filter: { // 过滤配置
          type: 'text', // 过滤类型
          defaultText: '',
          offset: 'right', // 过滤框是否右偏移
          fn: (filter) => { // 过滤回调方法
              //console.log(filter, 'infoCode','公告编号');
              this.param.infoCode = filter;
              this.getDataInit();
          }
      },
      hideTitle: true,
  }, 
  {
      field: 'infoName', // 显示字段
      title: '公告主题', // 表头显示
      width: '20%', // 单元格宽度
      hideTitle: false,
      flex: '1', // 单元格自适应宽度优先级,
      filter: { // 过滤配置
          type: 'text', // 过滤类型
          offset: 'right', // 过滤框是否左偏移
          fn: (filter) => { // 过滤回调方法
            //console.log(filter, 'infoName',"公告主题");
            this.param.infoName = filter;
            this.getDataInit();
          }
      },
      /*alignTd: 'center',
      alignTh: 'center',*/
  }, 
  {
      field: 'createName', // 显示字段
      title: '创建人', // 表头显示
      canSort: false, // 是否可排序
      width: '10%', // 单元格宽度
      isGroup: false,
      hideHeaderGroup: false,
      hideTitle: true,
      filter: { // 过滤配置
        type: 'text', // 过滤类型
        offset: 'right', // 过滤框是否左偏移
        fn: (filter) => { // 过滤回调方法
            //console.log(filter, 'index');
            this.param.createName = filter;
            this.getDataInit();
        }
          
      },
      /*alignTd: 'center',
      alignTh: 'center',*/
  }, 
  {
      field: 'infoStatus', // 显示字段
      title: '状态', // 表头显示
      width: '10%', // 单元格宽度
      hideTitle: true,
      filter: {
          type: 'checkList',
          optionList: [
            //待办状态0：未打开 1：未阅、未回复2：已阅、已回复 3：驳回 4：关闭
            {checked: false,name: '未阅',status:"1:0"},
            {checked: false,name: '已阅',status:"2:0"},
            {checked: false,name: '未回复',status:"1:1"},
            {checked: false,name: '已回复',status:"2:1"},
            {checked: false,name: '已关闭',status:"4:1"}
          ],
          option: {
          maxHeight: '150px',
          filterKey: 'name'
          },
          fn: (filter) => {
                //console.log(filter);
                let statusStr = '';
                if(filter.length!==6){
                    filter.map(function(item,i){
                        statusStr +=  (filter.length-1 === i ? item.status : item.status+',');    
                    });
                }
                this.param.statusAndType= statusStr;
                this.getDataInit();
          }
      }
  }, 
  {
    field: 'infoType', // 显示字段
    title: '类型', // 表头显示
    canSort: false, // 是否可排序
    width: '10%', // 单元格宽度
    hideTitle: true,
    filter: { // 过滤配置
        type: 'checkList',
            optionList: [
                {
                    checked: false,
                    name: '公告通知',
                    type:0
                }, {
                    checked: false,
                    name: '信息交流',
                    type:1
                }
            ],
            option: {
                maxHeight: '150px',
                filterKey: 'name'
            },
            fn: (filter) => {
                //console.log(filter, 'infoType');
                if(filter.length!==0){
                    this.param.type = (filter.length>1 ? '' : filter[0].type);
                }
                this.getDataInit();
            }
    },
    /*alignTd: 'center',
    alignTh: 'center',*/
}, 
{
  field: 'createTime', // 显示字段
  title: '创建时间', // 表头显示
  canSort: true, // 是否可排序
  width: '10%', // 单元格宽度
  hideTitle: true,
  /*alignTd: 'center',
  alignTh: 'center',*/
}, 
  {
        field: 'action', // 显示字段
        title: '操作', // 表头显示
        width: '15%', // 单元格宽度
        hideTitle: true
  }
];
tableData = [];

// @ViewChild('tableNew') tableNew;

    ngOnInit() {
        this.cookies= this.getCookie('userId');
        this.param.userId= this.cookies;
        this.pager = {
            total: 0,
            limit: 10,
            page: 1
        };
        this.getDataInit();
        
    }
    getCookie(name){
        let arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg))return decodeURI(arr[2]);
        else return null;
    }
    getDataInit(){
        this.http.getData({
        url: this.GetPathService.AjaxPath.bulletinServer+'/mineTodo/mineTodoList',
        search: this.param,
        }).subscribe(res => {
            if(res['returnCode'] === '0000'){
                this.tableOptions.loading = false;
                this.pager.total= res['result'].allRows;
                this.disposeData(res['result'].result);
            }else{
                this.tipService.show({ type: 'error', title: res['returnMessage']}); 
            }
        //this.annex = res['result'] && res['result'].demandAttach;
        }, err => {this.tipService.show({ type: 'error', title: "加载失败"});})
    }
  //处理数据
  disposeData(data){
    for(let i=0;i<data.length;i++){
      data[i].createTime = this.timeStamp(data[i].createTime);
      data[i].infoTypeName = (data[i].infoType === 0 ? '公告通知' : '信息交流');
      if(data[i].infoType === 0){
          //待办状态0：未打开 1：未阅、未回复2：已阅、已回复 3：驳回 4：关闭
          if(data[i].todoStatus === 0){//公告通知
            data[i].infoStatusName = '未打开'
          }else if(data[i].todoStatus === 1){
            data[i].infoStatusName = '未阅'
          }else if(data[i].todoStatus === 2){
            data[i].infoStatusName = '已阅'
          }else if(data[i].todoStatus === 4){
            data[i].infoStatusName = '已关闭'
          } 
      }else{
        if(data[i].todoStatus === 0){//公告通知
            data[i].infoStatusName = '未打开'
          }else if(data[i].todoStatus === 1){
            data[i].infoStatusName = '未回复'
          }else if(data[i].todoStatus === 2){
            data[i].infoStatusName = '已回复'
          }else if(data[i].todoStatus === 4){
            data[i].infoStatusName = '已关闭'
          }  
      }
    }
    this.tableData = data;
  }
  //时间戳转换成时间
  timeStamp(timestamp){
    let Y,M,D,h,m,s;
         let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        //var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        Y = date.getFullYear() + '-';
        M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate()) + ' ';
        h = (date.getHours() < 10 ? '0'+ date.getHours() : date.getHours()) + ':';
        m = (date.getMinutes() < 10 ? '0'+ date.getMinutes() : date.getMinutes()) + ':';
        s = (date.getSeconds()< 10 ? '0'+ date.getSeconds() : date.getSeconds());
        return Y+M+D+h+m+s;
  }
   //排序
   sortEvent(ev: any) {
    //console.log('sort', ev);
    this.pager = {
        total: 0,
        limit: 10,
        page: 1
      };
      this.param.sortField= ev.field;
      this.param.sorting= (ev.sort ==='desc' ? '-1' : (ev.sort === 'asc' ? '1' : ''));
      this.getDataInit();
  }
    //获取节点
    findItemByKey(dataList) {
        //console.log(dataList);
        const result = this.drawService.open({
            handler: (type) => { // 传入侧滑component的方法，在component中用@Input() handler:any;接收
                result.closeComponent();
                this.getDataInit();
            },
            data: dataList,
            component: ViewNoticeComponent, // 侧滑内容的component
            iconCls: '',
            title: ' 查看公告', // 侧滑是否有title
            padding: '10px 0 0 0', // 侧滑内容的padding值
            width: '80%', // 侧滑宽度
            hiddenScroll: false, // 隐藏侧滑的滚动条，使用程序自己的滚动条
        });
        
        
        //console.log(this.tableId)
        //console.log(this.tableId && this.tableNew.findItemByKey('id', this.tableId));
    }
    // 切换页码
    nowPageChange(ev) {
        this.pager.page = ev;
        this.param.page = ev;
        this.getDataInit();
        // this.getAdviceTableData();
    }

  
}
